package me.xplabs.scene 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import me.xplabs.AppFacade;
	import me.xplabs.battle.buff.Buff;
	import me.xplabs.Globals;
	import me.xplabs.build.UpgradeBuildingMediator;
	import me.xplabs.event.MMouseEvent;
	import me.xplabs.fight.MapManager;
	import me.xplabs.fight.layer.MagicLayer;
	import me.xplabs.interfaces.IScene;
	import me.xplabs.model.msg.MsgAddEffect;
	import me.xplabs.model.skill.MSkillInfo;
	import me.xplabs.msg.NotificationFight;
	import me.xplabs.msg.NotificationName;
	import me.xplabs.utils.Utils;
	import me.xplabs.view.FlagAreaMediator;
	import me.xplabs.view.GameMenuMediator;
	import me.xplabs.view.RaidInfoViewMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * ...
	 * @author xiaohan
	 */
	public class GameSceneMediator extends Mediator implements IScene
	{
		public static const NAME:String = "GameSceneMediator";
		private var _container:Sprite;
		private var _isInit:Boolean;
		// 战斗界面的渲染容器Bitmap
		private var _stageBitmap:Bitmap;
		// 战斗界面的渲染容器BitmapData
		private var _stageBitmapData:BitmapData;
		//是否开启渲染模式
		private var _isDrawing:Boolean;
		//战斗界面地图渲染
		private var _map:MapManager;
		private var _isAddListener:Boolean;
		private var _backBitmap:Bitmap;
		private var _frameIndex:int;
		
		private var _magicLayer:MagicLayer;
		public function GameSceneMediator( viewComponent:Object=null) 
		{
			super(NAME, viewComponent);
			
		}
		private function init():void 
		{
			if (_isInit) return;
			_isInit = true;
			_container = new Sprite();
			
			_stageBitmapData = new BitmapData(Globals.stageW, Globals.stageH, true, 0x00ff00ff);
			_stageBitmap = new Bitmap(_stageBitmapData);
			
			_map = new MapManager();
			_map.name = "Stage";
			_map.addEventListener(MMouseEvent.CLICK, mapClickHandler);
			
			_backBitmap = new Bitmap();
			_container.addChildAt(_backBitmap, 0);
			
			_magicLayer = new MagicLayer();
			
			
			if (!facade.hasMediator(UpgradeBuildingMediator.NAME)) facade.registerMediator(new UpgradeBuildingMediator(Globals.uiLayer));
			if (!facade.hasMediator(RaidInfoViewMediator.NAME)) facade.registerMediator(new RaidInfoViewMediator(Globals.uiLayer));
			if (!facade.hasMediator(GameMenuMediator.NAME)) facade.registerMediator(new GameMenuMediator(Globals.uiLayer));
			if (!facade.hasMediator(FlagAreaMediator.NAME)) facade.registerMediator(new FlagAreaMediator(Globals.uiLayer));
			/*_container.addEventListener(MouseEvent.CLICK, clickHandler);
			_container.addEventListener(MouseEvent.DOUBLE_CLICK, doubleClickHandler);
			_container.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			_container.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			_container.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			_container.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);*/
		}
		
		private function mapClickHandler(e:MMouseEvent):void 
		{
			//print(_map.hasMouseEvent);
			//print("点击舞台");
		}
		private function addListener():void
		{
			if (_isAddListener) return;
			_isAddListener = true;
			_container.doubleClickEnabled = true;
			_container.addEventListener(MouseEvent.CLICK, mouseEventHandler);
			_container.addEventListener(MouseEvent.DOUBLE_CLICK, mouseEventHandler);
			_container.addEventListener(MouseEvent.MOUSE_DOWN, mouseEventHandler);
			_container.addEventListener(MouseEvent.MOUSE_UP, mouseEventHandler);
		}
		
		private function delListener():void
		{
			if (!_isAddListener) return;
			_isAddListener = false;
			_container.doubleClickEnabled = false;
			_container.removeEventListener(MouseEvent.CLICK, mouseEventHandler);
			_container.removeEventListener(MouseEvent.DOUBLE_CLICK, mouseEventHandler);
			_container.removeEventListener(MouseEvent.MOUSE_DOWN, mouseEventHandler);
			_container.removeEventListener(MouseEvent.MOUSE_UP, mouseEventHandler);
		}
		
		private function mouseEventHandler(e:MouseEvent):void 
		{
			//Globals.eventType = e.type;
			switch(e.type)
			{
				case MouseEvent.CLICK:
					if (!_map.checkMouseEvent(MMouseEvent.CLICK))
					{
						sendMsg(NotificationName.CLICK_MAP_STAGE);
					}
					break;
				case MouseEvent.DOUBLE_CLICK:
					_map.checkMouseEvent(MMouseEvent.DOUBLE_CLICK);
					break;
				case MouseEvent.MOUSE_DOWN:
					_map.checkMouseEvent(MMouseEvent.MOUSE_DOWN);
					break;
				case MouseEvent.MOUSE_UP:
					_map.checkMouseEvent(MMouseEvent.MOUSE_UP);
					break;
			}
		}
		
		override public function handleNotification(notification:INotification):void 
		{
			switch(notification.getName())
			{
				case NotificationName.START_DRAWING:
					startDrawing();
					break;
				case NotificationName.SUSPEND_DRAWING:
					suspendDrawing();
					break;
				case NotificationName.FIGHT_NOTIFICATION:
					if (notification.getType() == NotificationFight.CHANGE_FIGHT) 
						_backBitmap.bitmapData = Globals.lib.getResObj(Globals.domain.background + notification.getBody());
					else if (notification.getType() == NotificationFight.USE_SKILL)
						_magicLayer.addMagic(MSkillInfo(notification.getBody()));
					else if (notification.getType() == NotificationFight.REMOVE_HIT_EFFECT)
						_magicLayer.delMagic(int(notification.getBody()));
					else if (notification.getType() == NotificationFight.ADD_EFFECT)
						_magicLayer.addEffect(MsgAddEffect(notification.getBody()));
					else if (notification.getType() == NotificationFight.DEL_EFFECT)
						_magicLayer.delEffect(int(notification.getBody()));
					else if (notification.getType() == NotificationFight.ADD_BUFF)
						_magicLayer.addBuff(Buff(notification.getBody()));
					else if (notification.getType() == NotificationFight.DEL_BUFF)
						_magicLayer.delMagic(int(notification.getBody()));
					else 
						_map.handleNotification(notification);
					break;
				default:
					break;
			}
		}
		/**
		 * 开启渲染
		 */
		private function startDrawing():void
		{
			if (_isDrawing) return;
			_isDrawing = true;
			Globals.gameManager.addUpdate(update);
		}
		/**
		 * 暂停渲染
		 */
		private function suspendDrawing():void
		{
			if (!_isDrawing) return;
			_isDrawing = false;
			Globals.gameManager.delUpdate(update);
		}
		/**
		 * 渲染刷新
		 */
		private function update():void
		{
			_frameIndex++;
			if (_frameIndex == 2) 
			{
				_stageBitmapData.lock();
				_stageBitmapData.fillRect(_stageBitmapData.rect, 0);
				_map.mouseX = container.mouseX;
				_map.mouseY = container.mouseY;
				_map.update(_stageBitmapData);
				_map.checkRollEvent();
				_frameIndex = 0;
				_magicLayer.update();
				_stageBitmapData.unlock();
			}
		}
		
		
		override public function listNotificationInterests():Array 
		{
			return [NotificationName.START_DRAWING, NotificationName.SUSPEND_DRAWING, NotificationName.FIGHT_NOTIFICATION];
		}
		public function show():void 
		{
			init();
			startDrawing();
			print("显示游戏视图");
			DisplayObjectContainer(viewComponent).addChild(_container);
			if(!_stageBitmap.parent) _container.addChild(_stageBitmap);
			if(!_magicLayer.parent) _container.addChild(_magicLayer);
			addListener();
			
			sendNotification(NotificationName.VIEW_SHOW_RAIDINFO);
		}
		
		public function dispose():void 
		{
			print("销毁游戏视图");
			if (DisplayObjectContainer(viewComponent).contains(_container)) 
				DisplayObjectContainer(viewComponent).removeChild(_container);
				if (_stageBitmap.parent) _stageBitmap.parent.removeChild(_stageBitmap);
				if (_magicLayer.parent) _magicLayer.parent.removeChild(_magicLayer);
				
			_map.dispose();
			_magicLayer.dispose();
			delListener();
			suspendDrawing();
			sendMsg(NotificationName.VIEW_HIDE_RAIDINFO);
			sendMsg(NotificationName.FIGHT_MAP_MANAGER, null, NotificationFight.QUIT_GAME);
		}
		
		public function get container():Sprite 
		{
			return _container;
		}
	}

}